﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.RabitsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.RabitsWay
{
    public class DelRabitsCommandHandler : IRequestHandler<DelRabitsCommand, ApiResult<int>>
    {
        IBaseReponsitory<Rabits> Rabits;

        public DelRabitsCommandHandler(IBaseReponsitory<Rabits> Rabits)
        {
            this.Rabits = Rabits;
        }

        public async Task<ApiResult<int>> Handle(DelRabitsCommand request, CancellationToken cancellationToken)
        {
            var list = await Rabits.GetQuery().FirstOrDefaultAsync(c => c.Rabit_Id == request.id);
            if (list != null)
            {
                list.Rabit_IsDel = true;
            }
            await Rabits.UpdateAsync(list);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "删除成功",
                Data = 1
            };
        }
    }
}
